package com.it.park.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.SensitiveWordService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.SensitiveWord;

@Controller
@RequestMapping("/admin/word")
public class AdminSensitiveWordController {
	private String rootPath=Constant.ADMIN_PATH;

	private Page _page = null;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(Integer page,Integer rows,HttpServletRequest request){
			Map<String,Object> map_ = new HashMap<>();
			Integer total = sensitiveWordService.selectTotal(map_);
				_page = new Page(page,total,rows);
			map_.put("startPos", _page.getStartPos());
			map_.put("pageSize", _page.getPageSize());
			List<SensitiveWord> list = sensitiveWordService.selectPagination(map_);
			Map<String,Object> map  =new HashMap<>();
			map.put("total", total);
			map.put("rows", list);
			return map;
	}
	
	@RequestMapping("wordList")
	public String linkList(HttpServletRequest request){
			return rootPath+"word/wordList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public JsonObj<String> save(SensitiveWord sensitiveWord,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<>();
		sensitiveWord.setCreateDate(new Date());
		boolean success = sensitiveWordService.insertSelective(sensitiveWord);
		if(success){
			json.setMessage("添加成功!");
			json.setSuccess(true);
		}else{
			json.setSuccess(false);
			json.setMessage("添加失败!");
		}
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj<String> deleteById(@RequestParam("id") int id,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<>();
		boolean success = sensitiveWordService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("删除失败!");
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj<String> edit(SensitiveWord sensitiveWord,HttpServletRequest request){
		JsonObj<String> json = new JsonObj<>();
		sensitiveWord.setCreateDate(new Date());
		boolean success = sensitiveWordService.updateByPrimaryKeySelective(sensitiveWord);
		if(success){
			json.setMessage("修改成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("修改失败!");
			json.setSuccess(false);
		}
		return json;
	}
}
